package cn.com.user.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.base.util.DateUtil;
import com.cabinet.service.ILatticeService;
import com.cabinet.service.IMCommoditiesService;
import com.cabinet.service.IUserOrderService;
import com.cabinet.service.dto.MyOrderDto;
import com.cabinet.web.entity.Commodities;
import com.cabinet.web.entity.Lattice;
import com.cabinet.web.entity.Order;
import cn.com.cabinet.dto.UserDto;
import cn.com.cabinet.service.UserServiceImpl;
import cn.com.easy.utils.CookieUtils;
/**
 * 订单处理类
 * @author hqz
 *
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private ILatticeService iLatticeService;
	@Autowired
	private UserServiceImpl userService;
	@Autowired
	private IUserOrderService iUserOrderService;
	
	@Autowired
	private IMCommoditiesService iMCommoditiesService;
	/**
	 * 检测商品数量
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	@RequestMapping("/check")
	@ResponseBody
	public int checkCommoditiesNum(Long latid,HttpServletRequest request) throws NumberFormatException, Exception{
		//System.out.println("订单检测："+latid);
		//Long cid = Long.valueOf(CookieUtils.getCookieValue(request, "cid"));
		Lattice lattice = iLatticeService.getLattice(latid);
		if(lattice.getLaNum()>0){
			return 1;
		}else{
			return 0;
		}	
	}
	
	@RequestMapping("/buy")
	@ResponseBody
	public Long buyCommodities(Long latid,HttpServletRequest request,HttpServletResponse response) throws Exception{
		//System.out.println(latid);
		//检测商品是否售空
		Lattice lattice = iLatticeService.getLattice(latid);
		if(lattice.getLaNum()>0){
			//检测用户是否登录
			String userid = CookieUtils.getCookieValue(request, "userid");
			if(userid == "null" || userid == null){
				//System.out.println("未登录");
				CookieUtils.setCookieRedirectUrl(request, response, "/commodities/detail?latid="+latid);
				return 0l;//用户未登录
			}else{
				//获取用户信息
				UserDto udto= userService.getUser(Long.valueOf(userid));
				Order o = new Order();
				o.setCabId(lattice.getCabId());
				o.setCommoditiesId(lattice.getCommoditiesId());
				o.setIsPay(1);//已支付
				o.setIsPick(0);//未取件
				o.setLatticeID(lattice.getId());
				o.setOrderPrice(lattice.getSalePrice());
				o.setRunNum(DateUtil.DateNum());
				o.setSubDate(new Date());
				o.setUserId(udto.getId());
				Order od = iUserOrderService.createOrder(o);
				//数量减一
				lattice.setLaNum(lattice.getLaNum()-1);
				iLatticeService.updateLattice(lattice);
				
				return od.getId();//购买成功
			}	
		}else{
			return 2l;//商品已售空
		}	
	}

	@RequestMapping("/buy/success")
	public String reditctOrder(Long oid,ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println("未登录");
			CookieUtils.setCookieRedirectUrl(request, response, "/index");
			return "redirect:/login";//用户未登录
		}else{
			Order o = iUserOrderService.getOrderById(oid);
			if(o == null){
				CookieUtils.setCookieRedirectUrl(request, response, "/index");
				return "redirect:/login";//用户未登录
			}
			//System.out.println(userid+"-----"+o.getUserId());
			if(o.getUserId().toString().equals(userid)){
				Lattice lattice = iLatticeService.getLattice(o.getLatticeID());
				Commodities commodities = iMCommoditiesService.getCommoditiesDetail(lattice.getCommoditiesId());
				modelMap.addAttribute("commoditiesDetail", commodities);
				modelMap.addAttribute("order", o);
				return "/order/order";
			}else{
				CookieUtils.setCookieRedirectUrl(request, response, "/index");
				return "redirect:/login";//用户未登录
			}
		}
	}
	
	@RequestMapping("/pickup")
	@ResponseBody
	public int orderPickup(Long oid,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println("未登录");
			CookieUtils.setCookieRedirectUrl(request, response, "/index");
			return 0;//用户未登录
		}else{
			Order o = iUserOrderService.getOrderById(oid);
			if(o == null){
				CookieUtils.setCookieRedirectUrl(request, response, "/index");
				return 0;//用户未登录
			}
			//System.out.println(userid+"-----"+o.getUserId());
			if(o.getUserId().toString().equals(userid)){
				if(o.getIsPick() == 0){
					o.setIsPick(1);
					iUserOrderService.updateOrder(o);
					return 1;
				}else{
					return 2;
				}
				
			}else{
				CookieUtils.setCookieRedirectUrl(request, response, "/index");
				return 0;//用户未登录
			}
		}
	}
	
	@RequestMapping("/myorder")
	public String myOrder(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println("未登录");
			CookieUtils.setCookieRedirectUrl(request, response, "/order/myorder");
			return "redirect:/login";//用户未登录
		}else{
			List<MyOrderDto> myOrder = iUserOrderService.getOrderByUserId(Long.valueOf(userid));
			modelMap.addAttribute("myOrder", myOrder);
			return "/user/user_order";
		}
	}
	
}
